home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dpmigcc5.zip / RSX / SOURCE / DEB / BREAKP.C < prev    next >
C/C++ Source or Header  |  1994-05-27  |  2KB  |  118 lines

  1. /*
  2. ** Breakpoints with ptrace()
  3. **
  4. ** (c) Rainer Schnitker 1994
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #ifndef __GO32__
  10.  #include <process.h>
  11.  #include <sys/ptrace.h>
  12.  #include <sys/user.h>
  13.  #include <sys/reg.h>
  14. #else  /* go32 */
  15.  #include <sys\_process.h>
  16.  #include <sys\_ptrace.h>
  17.  #include <sys\_user.h>
  18.  #include <sys\_reg.h>
  19. #endif
  20.  
  21. #include "breakp.h"
  22.  
  23. struct breakpoint bp[MAX_BP];
  24.  
  25. static void insert_int3(struct breakpoint * bp, int pid)
  26. {
  27.     int r;
  28.     errno = 0;
  29.     r = ptrace(PTRACE_PEEKTEXT, pid, bp->addr, 0);
  30.     if (errno != 0)
  31.     return;
  32.     bp->save = (unsigned char) r;
  33.     r = (r & ~0xff) | 0xcc;
  34.     ptrace(PTRACE_POKETEXT, pid, bp->addr, r);
  35. }
  36.  
  37. static void remove_int3(struct breakpoint * bp, int pid)
  38. {
  39.     int r;
  40.  
  41.     errno = 0;
  42.     r = ptrace(PTRACE_PEEKTEXT, pid, bp->addr, 0);
  43.     if (errno != 0)
  44.     return;
  45.     r = (r & ~0xff) | bp->save;
  46.     ptrace(PTRACE_POKETEXT, pid, bp->addr, r);
  47. }
  48.  
  49. void insert_breakpoints(int pid)
  50. {
  51.     int i;
  52.     for (i = 0; i < MAX_BP; i++)
  53.     if (bp[i].status == BP_ENABLE)
  54.         insert_int3(bp+i, pid);
  55. }
  56.  
  57. void remove_breakpoints(int pid)
  58. {
  59.     int i;
  60.     for (i = 0; i < MAX_BP; i++)
  61.     if (bp[i].status == BP_ENABLE)
  62.         remove_int3(bp+i, pid);
  63. }
  64.  
  65. int set_bp(int addr)
  66. {
  67.     int i;
  68.     for (i = 1; i < MAX_BP; i++)
  69.     if (!(bp[i].status))
  70.         break;
  71.     if (i == MAX_BP) {
  72.     printf("no break free\n");
  73.     return -1;
  74.     }
  75.     bp[i].addr = addr;
  76.     bp[i].status = BP_ENABLE;
  77.     return i;
  78. }
  79.  
  80. int delete_bp(int no)
  81. {
  82.     if (no >= MAX_BP || bp[no].status == 0) {
  83.     printf("invalid break-no\n");
  84.     return -1;
  85.     }
  86.     bp[no].addr = 0;
  87.     bp[no].status = BP_UNDEF;
  88.     return 0;
  89. }
  90.  
  91. int disable_bp(int no)
  92. {
  93.     if (no <= 1 && no >= MAX_BP) {
  94.     printf("ivalid break-no\n");
  95.     return -1;
  96.     }
  97.     if (bp[no].status == BP_DISABLE) {
  98.     printf("already disabled\n");
  99.     return -1;
  100.     }
  101.     bp[no].status = BP_DISABLE;
  102.     return 0;
  103. }
  104.  
  105. int enable_bp(int no)
  106. {
  107.     if (no <= 1 && no >= MAX_BP) {
  108.     printf("ivalid break-no\n");
  109.     return -1;
  110.     }
  111.     if (bp[no].status == BP_ENABLE) {
  112.     printf("already enabled\n");
  113.     return -1;
  114.     }
  115.     bp[no].status = BP_ENABLE;
  116.     return 0;
  117. }
  118.